package com.springboot.sba.D9_DynamicTask.config;

import com.springboot.sba.D9_DynamicTask.task.Task1;
import com.springboot.sba.D9_DynamicTask.task.Task2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;

/**
 * @description:
 * @author: ZZZ
 * @time: 2021/8/8 15:42
 */
@Component
public class ScheduledTask1 {
    @Value("${scheduleTask.cron1}")
    private String cron1;
    @Value("${scheduleTask.cron2}")
    private String cron2;
    @Value("${task1.enabled}")
    private boolean taskEnable;

    @Autowired
    private ThreadPoolTaskScheduler threadPoolTaskScheduler;
    private ScheduledFuture<?> task1future;
    private ScheduledFuture<?> task2future;
    public  ConcurrentHashMap<String, ScheduledFuture>  taskSchedulerMap= new ConcurrentHashMap<String, ScheduledFuture>();
    //初始化
    @PostConstruct
    private void postConstruct() {
        startCron();
    }
    //销毁
//    @PreDestroy
//    private void preDestroy() {
//        stopCron();
//    }


    //项目启动的时候就启动定时任务，初始的corn可以去数据库里读取
    private void startCron() {
        if(!taskEnable){
            System.out.println("task1 定时任务未开启");
            return;
        }
        task1future = threadPoolTaskScheduler.schedule(new Task1(), new CronTrigger(cron1));
        taskSchedulerMap.put("task1",task1future);
        task2future = threadPoolTaskScheduler.schedule(new Task2(), new CronTrigger(cron2));
        taskSchedulerMap.put("task2",task2future);
    }

    //修改cron表达式
    public void changeCron(String taskKey,String cron) {
        if(!taskEnable){
            System.out.println("task1 定时任务未开启");
            return;
        }
        stopCron(taskKey);
        if("task1".equals(taskKey)){
            task1future = threadPoolTaskScheduler.schedule(new Task1(), new CronTrigger(cron));
            taskSchedulerMap.put("task1",task1future);
        }
        if("task2".equals(taskKey)){
            task2future = threadPoolTaskScheduler.schedule(new Task2(), new CronTrigger(cron));
            taskSchedulerMap.put("task2",task2future);
        }
    }

    //关闭定时任务
    public void stopCron(String taskKey) {
        if(!taskEnable){
            System.out.println("task1 定时任务未开启");
            return;
        }
        if(taskSchedulerMap.containsKey(taskKey)){
            ScheduledFuture scheduledFuture = taskSchedulerMap.get(taskKey);
            if (scheduledFuture != null) {
                scheduledFuture.cancel(true);
            }
            // 查看任务是否在正常执行之前结束,正常true
            boolean cancelled = scheduledFuture.isCancelled();
            while (!cancelled) {
                scheduledFuture.cancel(true);
            }
            taskSchedulerMap.remove(taskKey);
        }
    }
}
